﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InstituteManager.Data;
using System.Data;

namespace InstituteManager.Bussiness
{
    public static class StudentBL
    {
        public static string errorMsg = "";
        public static List<tbl_Student> getByAll()
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            List<tbl_Student> exhi = InsMana.tbl_Student.ToList();
            return exhi;
        }

        public static tbl_Student getById(int Id)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return InsMana.tbl_Student.First(temp => temp.StudentId == Id);
        }

        public static tbl_Student getByUsername(string Username)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return InsMana.tbl_Student.First(temp => temp.Username == Username);
        }

        public static bool AddNew(tbl_Student ex)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                // command to cach exception username exists
                try{
                    InsMana.tbl_Student.First(temp => temp.Username == ex.Username);
                    errorMsg = "Username is exists ! ";
                    return false;
                }catch(Exception){
                    InsMana.AddTotbl_Student(ex);
                }                    

                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool Update(tbl_Student ex)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                tbl_Student obj = InsMana.tbl_Student.First(temp => temp.StudentId == ex.StudentId);
                obj.StudentName = ex.StudentName;
                obj.DOB = ex.DOB;
                obj.StudentPhone = ex.StudentPhone;
                obj.Email = ex.Email;
                obj.Address = ex.Address;
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool Delete(int Id)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                InsMana.DeleteObject(InsMana.tbl_Student.First(temp => temp.StudentId == Id));
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool checkLogin(string user, string pass)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                InsMana.tbl_Student.First(temp => temp.Username == user && temp.Password == pass);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static List<tbl_Student> SearchByName(string StudentName)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();

            return (from sc in InsMana.tbl_Student where sc.StudentName.Contains(StudentName) select sc).ToList<tbl_Student>();
        }

        public static bool changePassword(string Username, string NewPassword)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                (from sc in InsMana.tbl_Student where sc.Username == Username select sc).First().Password = NewPassword;
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            return true;
        }

    }
}
